﻿using System;
using System.IO;
using System.Text;
using System.Windows.Forms;

namespace AnalyticsLyrics
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void BT_Open_Click(object sender, EventArgs e)
        {
            OpenFileDialog open = new OpenFileDialog();
            if (open.ShowDialog() == DialogResult.OK)
            {
                TB_File.Text = open.FileName;
                StreamReader reader = new StreamReader(open.FileName);
                TB_Out.Text = "";
                string sOut = "";
                int iType = 0;
                while (reader.Peek() >= 0)
                {
                    string s = reader.ReadLine().Trim();

                    //if (s=="[Script Info]")
                    //{
                    //    iType = 1;
                    //    continue;
                    //}

                    //if (s=="[V4+ Styles]")
                    //{
                    //    iType = 2;
                    //}

                    //if (s=="[Events]")
                    //{
                    //    iType = 3;
                    //}

                    //if (iType == 1)
                    //{
                    //    sOut += s + "\n";
                    //}

                    //continue;
                    if (!s.StartsWith("Dialogue:")) continue;
                    string[] arr = s.Split(',');
                    string sStart = arr[1];
                    string sEnd = arr[2];
                    string sText= arr[9];;
                    if (arr.Length > 9)
                    {
                        for (int i = 10; i < arr.Length; i++)
                        {
                            sText +="," + arr[i];
                        }
                    }
                    if (sOut != "") sOut += ",";
                    sOut += "[" + AnalyticsTime(sStart) + ","  + AnalyticsTime(sEnd) + ",\"" + sText + "\"]\n";
                }

                TB_Out.Text=sOut;
            }
        }

        private static string AnalyticsTime(string s )
        {
            string[] arr = s.Split(':');
            return (double.Parse(arr[0])*3600 + double.Parse(arr[1])*60 + double.Parse(arr[2].Replace('.',','))).ToString().Replace(',','.');
        }

        private void BT_Save_Click(object sender, EventArgs e)
        {
            SaveFileDialog save = new SaveFileDialog();
            save.Filter = "JSON|*.json|All file|*.*";
            if (save.ShowDialog() == DialogResult.OK)
            {
                StreamWriter writer = new StreamWriter(save.FileName, false, Encoding.UTF8);
                writer.Write(TB_Out.Text);
                writer.Close();
            }
        }
    }
}
